<?php
error_reporting(0);
/**
 * GameController
 * 
 * @author
 * @version 
 */
require_once 'Zend/Controller/Action.php';
class PageController extends Sunnet_Controller_Action
{
    var $_arrParam;
    protected $domain = DOMAIN;
    public function init()
    {
        //      Truyền các tham số nhận được ra view
        $this->_arrParam = $this->_request->getParams();
        if (empty($this->_arrParam['page'])) {
            $this->_arrParam['page'] = 1;
        }
        if (empty($this->_arrParam['line_per_page'])) {
            $this->_arrParam['line_per_page'] = 10;
        }

        $this->view->arrParam = $this->_arrParam;
        $this->view->domain = $this->domain;
        $this->view->function = $this;
    }

    public function preDispatch(){
        $template_path = TEMPLATE_PATH . "/wap/default/";
        $this->loadTemplate($template_path,'template.ini','template');
         //set group main
        Zend_Loader::loadClass('Wap_Models_ControllerModel');
        $controllerModel = new Wap_Models_ControllerModel();       
        $this->view->groupmain_list = $controllerModel->listMenuGroupNote();
        $this->view->newsHots = $controllerModel->listNewsHotRight();
        $this->view->listBanners = $controllerModel->listBannerHeader();

        //      Load js và css
        $this->view->headLink()->appendStylesheet('/css/style.css','screen');
        $this->view->headLink()->appendStylesheet('/css/verify.css','screen');
    }

    public function indexAction (){
//      Load js và css
        $this->view->headLink()->appendStylesheet('/css/style.css','screen');
        //list note group
 
        $params = $this->_request->getParams();

        Zend_Loader::loadClass('Wap_Models_PageModel');
        $mPost= new Wap_Models_PageModel();        
        //load info post
        $post = $mPost->getPageID(intval($params['id']));
        $this->view->post = $post;
        
        //load tin tuc lien quan
        $releasePost = $mPost->getListPageNotIdView(intval($params['id']));
        $this->view->idpost = intval($params['id']);
        $this->view->releasePost = $releasePost;

        //SEO POST        
        if(!empty($post['seo_title'])) $this->view->headTitle()->headTitle($post['seo_title']);
        else $this->view->headTitle()->headTitle($post['name']);
        if(!empty($post['seo_keywords'])) $this->view->headMeta()->appendName('keywords', $post['seo_keywords']);
        if(!empty($post['seo_description'])) $this->view->headMeta()->appendName('description', $post['seo_description']);
    }

    public function lienHeAction (){

        
        Zend_Loader::loadClass('Wap_Models_FeedBackModel');
        $mFeedback= new Wap_Models_FeedBackModel();    
        //check action Post change
        $this->view->actionsucc = "";
        $param= $this->_request->getParams();
        if ($this->_request->isPost()){         

                if($param){

                    $messager = $param ['messager'];
                    $messager = addslashes( $messager );
                    $email = $param ['email'];
                    $email = addslashes( $email );
                    $phone = $param ['phone'];
                    $phone = addslashes( $phone );
                    //insert data

                    $data = array(
                        'messager' => $messager, 
                        'email' => $email, 
                        'phone' => $phone, 
                    );                

                    $mFeedback->add($data); 
                    //Process completed, acction redirect to main account
                    $this->_redirect('/page/lien-he?actionsucc=successfull');    

                } //end if

                     
        }
        if(isset($param ['actionsucc'])) $this->view->actionsucc = "successfull";   
    }

    public function dangKyAction (){

        //check action Post change
        $param= $this->_request->getParams();
        $thongbao ="";

        if ($this->_request->isPost()){         

                if($param){

                    Zend_Loader::loadClass('Wap_Models_UserModel');

                    $user=new Wap_Models_UserModel();
                    $UserID = addslashes($param ['UserID']);

                    $Password = addslashes($param ['Password']);
                    $userID_validator = new Zend_Validate_Db_RecordExists(
                                    array(
                                        'table' => 'user',
                                        'field' => 'UserID'
                                    )
                    );

                    ///check chua ton tai email thi insert

                    if (!$userID_validator->isValid($UserID)){

                        $user->insert(array('UserID'    => $UserID,

                                          'Password'       => sha1($Password)
                         ));  

                        //Process completed, acction redirect to main account

                        $this->_redirect(DOMAIN);     

                    }else {
                        $thongbao = "Tên đăng nhập đã có người sử dụng";
                    }                                    
                }
        }

        $this->view->thongbao = $thongbao;        
    }
    
    public function dangNhapAction (){
//      Load js và css
        // $this->_helper->viewRenderer->setNoRender();
        // $this->_helper->getHelper('layout')->disableLayout();
        // $this->_helper->layout->disableLayout();

        //check action Post change
        $param= $this->_request->getParams();
        $thongbao ="";

        if ($this->_request->isPost()){         

                if($param){

                    Zend_Loader::loadClass('Wap_Models_UserModel');
                    $user=new Wap_Models_UserModel();

                    $UserID = addslashes($param ['name']);                    
                    $Password = sha1(addslashes($param ['password']));
                    $return_check = false;

                    $check_user = $user->checkUser($UserID, $Password);

    //              Zend_Debug::dump($check_user);die;
                    if ($check_user) {
                        $auth = Zend_Auth::getInstance();
                        $auth->getStorage()->write(array('UserID'   => $check_user['UserID']));
                        Zend_Session::rememberMe(); 

                        // $session = new Zend_Session_Namespace();
                        // $session->UserID = $check_user['UserID'];
                        if($param ['red']==1) $this->_redirect("/tool/dang-ky-ho-so-online");
                        elseif($param ['red']==2) $this->_redirect("/account/theo-doi-ho-so");
                        else $this->_redirect(DOMAIN);
                    }
                    else {
                        $thongbao = "Tên đăng nhập hoặc mật khẩu không đúng";
                    }                                                 
                                     
                }
            
        }

        $this->view->thongbao = $thongbao;        
    }

    public function thoatAction()
    {
        $auth = Zend_Auth::getInstance();
        $auth->clearIdentity();
        $curr_url = $this->_request->getParam('curr_url');
                
        if ($curr_url){
            $this->_redirect($curr_url);
        }else{
            $this->_redirect(DOMAIN);
        }
    }

    //code date time
    public function convertTimeAgo ($activity_time){       
       $timeinsecond = time() - $activity_time;
       if($timeinsecond >=0){
         $minus  = abs(floor(($timeinsecond%3600)/60));
         $hour   =   abs(round(($timeinsecond-$minus*60)/3600,0));              
          
         $timematch = 0;
         if($timeinsecond<60){
            $timematch = $timeinsecond." giây trước";  
            return $timematch;
         }elseif(60<$timeinsecond && $timeinsecond<3600){
            $timematch = $minus." phút trước";      
            return $timematch;       
         }elseif(3600<=$timeinsecond && $timeinsecond<86400){
            $timematch = $hour." giờ trước"; 
            return $timematch;
         }elseif(86400<=$timeinsecond && $timeinsecond<518400){
            $timematch =  abs(round(($hour/24))) . " ngày trước";
            return $timematch;
         }else {
            $timematch= date('d/m/Y', $activity_time);
            return $timematch;
         }
         return $timematch;
       }
    }
    function cut_substr($str,$n)
    {       
        $str = strip_tags($str);   
        $str = trim($str);    
       // ----- remove control characters ----- 

        $str = str_replace("\r", '<br/>', $str);    // --- replace with empty space
        $str = str_replace("\n", '', $str);   // --- replace with space
        $str = str_replace("\t", '', $str);   // --- replace with space
        // ----- remove cac br lien tiep ----- 
        $str = preg_replace('#(<br */?>\s*)+#i', '<br />', $str);

        $html = substr($str,0,$n);
        $html = substr($html,0,strrpos($html,' '));

        return $html.'...';
    }  

    function cut_substr_strip($str)
    {                   
        $str = strip_tags($str);   
        $str = trim($str);    
       // ----- remove control characters ----- 

        $str = str_replace("\r", '<br/>', $str);    // --- replace with empty space
        $str = str_replace("\n", '', $str);   // --- replace with space
        $str = str_replace("\t", '', $str);   // --- replace with space
        // ----- remove cac br lien tiep ----- 
        $str = preg_replace('#(<br */?>\s*)+#i', '<br />', $str);

        return $str;

    }  

}
